import {Component, OnInit} from '@angular/core';
import {UserService} from '../user.service';
import {DatePipe} from '@angular/common';

@Component({
  selector: 'app-user-manage',
  templateUrl: './user-manage.component.html',
  styleUrls: ['./user-manage.component.scss']
})
export class UserManageComponent implements OnInit {

  query = {
    search: '',
    date: [],
    certificationType: '',
    page: 1,
    pageSize: 10,
  };
  // 表格数据
  listOfData = {
    page: 1,
    total: 100,
    records: []
  };
  detailModal = {
    visible: false,
    id: '',
    type: '',
    records: [] as any,
    total: 0,
    page: 1,
    pageSize: 10,
  };
  POINTSTYPE = [
    '',
    '充值',
    '签到',
    '积分兑换点豆',
    '积分抽奖',
    '积分兑换礼品',
  ];

  constructor(
    public userService: UserService,
    public datePipe: DatePipe
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }


  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getTableData();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getTableData();
  }

  getTableData() {
    this.userService.getUserList({
      search: this.query.search,
      createBeginTime: this.datePipe.transform(this.query.date[0], 'yyyy-MM-dd'),
      createEndTime: this.datePipe.transform(this.query.date[1], 'yyyy-MM-dd'),
      certificationType: this.query.certificationType,
      page: this.query.page,
      pageSize: this.query.pageSize,
    }).then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  queryBtn() {
    this.query.page = 1;
    this.getTableData();
  }

  resetCondition() {
    this.query = {
      search: '',
      date: [],
      certificationType: '',
      page: 1,
      pageSize: 10,
    };
    this.getTableData();
  }

  onDetailModalPageIndexChange(index: number) {
    this.detailModal.page = index;
    this.getDetailTable();
  }

  onDetailModalPageSizeChange(pageSize: number) {
    this.detailModal.pageSize = pageSize;
    this.getDetailTable();
  }

  showDetail(id: any, type: 'growth' | 'integral') {
    this.detailModal.type = type;
    this.detailModal.id = id;
    this.detailModal.records = [];
    this.detailModal.visible = true;
    this.getDetailTable();
  }

  getDetailTable() {
    if (this.detailModal.type === 'growth') {
      this.userService.getGrowthList({
        id: this.detailModal.id,
        page: this.detailModal.page,
        pageSize: this.detailModal.pageSize
      }).then((res: any) => {
        if (res.code === 0) {
          this.detailModal.records = res.data.records;
          this.detailModal.total = res.data.total;
        }
      });
    }
    if (this.detailModal.type === 'integral') {
      this.userService.getIntegralList({
        id: this.detailModal.id,
        page: this.detailModal.page,
        pageSize: this.detailModal.pageSize
      }).then((res: any) => {
        if (res.code === 0) {
          this.detailModal.records = res.data.records;
          this.detailModal.total = res.data.total;
        }
      });
    }
  }

  hideDetailModal() {
    this.detailModal = {
      visible: false,
      id: '',
      type: '',
      records: [],
      total: 0,
      page: 1,
      pageSize: 10,
    };
  }
}
